package bw.hackathon.ub;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import bw.hackathon.ub.CompleteTest.sendResults;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class search extends Activity{
	public static final String ID="Id";

	private EditText search;

	private ProgressDialog pDialog;
	JSONParser jsonParser = new JSONParser();
	private static String url_create_product = "http://www.tsbayford.com/android_connect/search_id.php";
	private static final String TAG_SUCCESS = "success";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.search_page);
		
		search = (EditText) findViewById(R.id.searche);
		
		Button searchPatientButton = (Button) findViewById(R.id.search_page1);
		searchPatientButton.setOnClickListener(new View.OnClickListener()
		{		
		@Override
		public void onClick(View v) {
			
			new CreateNewProduct().execute();
			
		}
	});
	}
	class CreateNewProduct extends AsyncTask<String, String, String> {

		/**
		 * Before starting background thread Show Progress Dialog
		 * */
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			pDialog = new ProgressDialog(search.this);
			pDialog.setMessage("Checking Id....");
			pDialog.setIndeterminate(false);
			pDialog.setCancelable(true);
			pDialog.show();
		}

		/**
		 * Creating product
		 * */
		protected String doInBackground(String... args) {
			
			String idd = search.getText().toString();

			// Building Parameters
			List<NameValuePair> params = new ArrayList<NameValuePair>();
			params.add(new BasicNameValuePair("id", idd));

			// getting JSON Object
			// Note that create product url accepts POST method
			JSONObject json = jsonParser.makeHttpRequest(url_create_product,
					"POST", params);
			
			// check log cat for response
			Log.d("Create Response", json.toString());

			// check for success tag
			try {
				int success = json.getInt(TAG_SUCCESS);

				if (success == 1) {
					// successfully created product
					Intent k = new Intent(getApplicationContext(), Add_edit_patient.class);
					k.putExtra("name", json.getString("name"));
					k.putExtra("emailaddr", json.getString("emailaddr"));
					k.putExtra("phonenumber", json.getString("phonenumber"));
					k.putExtra("dob", json.getString("dob"));
					k.putExtra("addr", json.getString("addr"));
					k.putExtra("edit", "true");
					k.putExtra("id",idd);
					startActivity(k);
					
					// closing this screen
					
				} else {
					// failed to create product
					Intent i = new Intent(getApplicationContext(), TestReady.class);
					i.putExtra("message", "Invalid Id or Connection Failure Please Try Again");
					startActivity(i);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			return null;
		}

		/**
		 * After completing background task Dismiss the progress dialog
		 * **/
		protected void onPostExecute(String file_url) {
			// dismiss the dialog once done
			pDialog.dismiss();
		}

	
	
	}
}


	
